import { getMember } from './activityListServ'
import { parse } from 'qs'
import { message } from 'antd'
import moment from 'moment'
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
import { hashHistory } from 'dva/router'
import { makeAddress } from "utils/util"
import {queryAllAddress} from "../../mideaRealtyAdmin/buildingAdmin/baseAdmin/baseinfo/baseinfoServ";
const defultState = {
  loading: false,
  btnLoading: false,
  xxx: '1',
  biz:{
    queryfrom:{}
  }
}

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {

        if('' + tmpModule.pathname === '' + pathname){
       //   dispatch({  type: 'queryInfo', payload: {} })
            dispatch({ type: 'getAreaList', payload: {} });
        }
      })
    },
  },

  effects: {
    // 查询记录
    *queryInfo({ payload }, { put, call, select }) {
      try{

        let tmpState = yield select(state => state[tmpModule.namespace])
        //调用接口，并传参
        const { data , resultCode , resultMsg } = yield call(queryAllAddress, parse({ xxx: "1" }))
        // 调完接口，将返回的数据存入到状态机
        yield put({ // 调用reducers中方法，把新数据与状态机数据合并更新
          type: 'updateStore', // 方法名，也可以调用effects中的方法
          payload:{xxx: data}
        })
      }catch(e){
        console.error(e)
      }
    },
      *getAreaList({ payload }, { put, call, select }){
          const { data, success } = yield call(queryAllAddress);
          if(success) {
              let areaList = []
              let arr = [data[0]]
              let attr = [data[data.length-1]]
              data[0] = {
                  label:"北京",
                  value:"110100",
                  children:arr
              }
              data[data.length-1]={
                  label:"澳门",
                  value:"820000",
                  children:attr
              }
              //去掉第三级，
              areaList = makeAddress(data,2)
              yield put({ type: 'updateStore',payload:{areaList}})
          }
      }

  },

  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
